package com.honda.util;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class ResultUtils {

	protected final Logger log = LoggerFactory.getLogger(ResultUtils.class);

	protected void writeJson(HttpServletResponse response, String result) {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter writer = null;
		try {
			writer = response.getWriter();
			writer.write(result);
		} catch (Exception e) {
			log.error("error:", e);
		} finally {
			if (writer != null) {
				writer.close();
			}
		}
	}

	/**
	 * 
	 * @Title: jsonResult   
	 * @Description: (接口返回标准)   
	 * @param: @param status 状态码
	 * @param: @param code   code值
	 * @param: @param msg    消息
	 * @param: @param data   业务数据
	 * @param: @param response      
	 * @return: void      
	 * @throws
	 */
	protected void jsonResult(int status, String code, String msg, Object data,
			HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", status);
		map.put("code", code);
		map.put("msg", msg);
		map.put("result", data);
		writeJson(response, JSONObject.toJSONString(map,
				SerializerFeature.WriteMapNullValue));
	}

	protected void jsonResult1(Object data, HttpServletResponse response) {
		writeJson(response, JSONObject.toJSONString(data,
				SerializerFeature.WriteMapNullValue));
	}
}
